package com.athuahua.spzx.service.impl;

import com.alibaba.fastjson.JSON;
import com.athuahua.spzx.dto.product.SkuSaleDto;
import com.athuahua.spzx.entity.ProductDetails;
import com.athuahua.spzx.feignapi.UserFeignClient;
import com.athuahua.spzx.vo.ProductSkuVo;
import com.athuahua.spzx.dto.h5.ProductSkuDto;
import com.athuahua.spzx.entity.ProductSku;
import com.athuahua.spzx.entity.vo.ProductVo;
import com.athuahua.spzx.mapper.ProductDetailsMapper;
import com.athuahua.spzx.mapper.ProductSkuMapper;
import com.athuahua.spzx.vo.h5.ProductItemVo;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.athuahua.spzx.entity.Product;
import com.athuahua.spzx.service.ProductService;
import com.athuahua.spzx.mapper.ProductMapper;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author huahua
 * @description 针对表【product(商品)】的数据库操作Service实现
 * @createDate 2024-08-12 16:43:01
 */
@Service
public class ProductServiceImpl extends ServiceImpl<ProductMapper, Product>
        implements ProductService {

    @Resource
    private ProductMapper productMapper;
    @Resource
    private ProductSkuMapper productSkuMapper;
    @Resource
    private ProductDetailsMapper productDetailsMapper;

    @Resource
    private UserFeignClient userFeignClient;

    @Override
    public IPage<ProductSkuVo> PageInfo(Integer page, Integer limit, ProductSkuDto productSkuDto) {
        IPage<ProductVo> iPage = new Page<>(
                page,
                limit
        );
        IPage<ProductSkuVo> productVos = productMapper.IpageInfoSku(iPage, productSkuDto);
        return productVos;
    }

    @Override
    public ProductItemVo item(Long skuId) {
        //0.记录浏览历史
        userFeignClient.saveUserBrowseHistory(skuId);

        //1.创建一个vo对象，用于封装最终数据
        ProductItemVo result = new ProductItemVo();

        //2.查询商品sku信息
        ProductSku productSku = productSkuMapper.selectById(skuId);

        //3.根据第二步获取的productId,获取商品信息
        Long productId = productSku.getProductId();
        Product product = productMapper.selectById(productId);

        //4.根据productId获取商品详情详细

        ProductDetails productDetails = productDetailsMapper
                .selectOne(new LambdaQueryWrapper<ProductDetails>()
                        .eq(ProductDetails::getProductId, productId)
                );


        //5.封装map集合，商品规格对应商品skuId信息
        Map<String,Object> skuSpecValueMap = new HashMap<>();
        //根据商品id获取商品所有sku列表
        productSkuMapper.selectList(new LambdaQueryWrapper<ProductSku>()
                .eq(ProductSku::getProductId, productId)
        ).forEach(item -> {
            skuSpecValueMap.put(item.getSkuSpec() ,item.getId());
        });

        //6.封装最终数据
        result.setProduct(product);
        result.setProductSku(productSku);
        result.setSkuSpecValueMap(skuSpecValueMap);

        //封装详情图片
        String imageUrls = productDetails.getImageUrls();
        String[] split = imageUrls.split(",");
        List<String> list = Arrays.asList(split);
        result.setDetailsImageUrlList(list);
        result.setSliderUrlList(Arrays.asList(product.getSliderUrls().split(",")));
        result.setSpecValueList(JSON.parseArray(product.getSpecValue()));

        return result;
    }

    @Override
    public ProductSku getBySkuId(Long skuId) {
        return productSkuMapper.selectById(skuId);
    }

    @Override
    public Boolean updateSkuSaleNum(List<SkuSaleDto> skuSaleDtoList) {
        if(!CollectionUtils.isEmpty(skuSaleDtoList)) {
            for(SkuSaleDto skuSaleDto : skuSaleDtoList) {
                ProductSku productSku = productSkuMapper.selectOne(new LambdaQueryWrapper<ProductSku>()
                        .eq(ProductSku::getId, skuSaleDto.getSkuId()));
                productSku.setSaleNum(productSku.getSaleNum() + skuSaleDto.getNum());
                productSkuMapper.updateById(productSku);
            }
        }






        return null;
    }
}




